//
//  NetManager.m
//  Fx678htnews
//
//  Created by w John on 11-8-25.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "AFNetManager.h"

static AFNetManager *netManager = nil;

@implementation AFNetManager
@synthesize retstr;

+(AFNetManager*) manager{
    @synchronized(self){
        if (netManager == nil) {
            netManager = [[self alloc] init];
        }
    }
    return netManager;
}

-(NSString*) httpRequest:(NSString *) urlString{

    //NSLog(@"----- http url = %@", urlString);
    // 初始化请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];         
    // 设置URL
    [request setURL:[NSURL URLWithString:urlString]];
    [request setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];  
    // 设置HTTP方法
    [request setHTTPMethod:@"GET"];
    
    //request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
    //request.HTTPShouldHandleCookies = NO;
    //request.HTTPShouldUsePipelining = YES;
    
    NSHTTPURLResponse *response;
    
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request 
                                               returningResponse:&response error:nil]; 
    int statusCode =  (int)[response statusCode];
    
    NSString* strRet = nil;
    if (statusCode == 200) {
        strRet = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    } else {
        // 服务器请求失败
        strRet = @"-100";
    }

    // 释放对象
    //[request release];
    //return [strRet autorelease];
    return strRet;
}

-(NSString*) httpRequestPost:(NSString *) urlString postString:(NSString *) postString
{
    NSData *postData = [postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
    NSString *postLength = [NSString stringWithFormat:@"%d", (int)[postData length]];
    
    // 初始化请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];         
    // 设置URL
    [request setURL:[NSURL URLWithString:urlString]];
    [request setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];  
    // 设置HTTP方法
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];  
    [request setHTTPBody:postData];
    
    NSHTTPURLResponse *response;
    
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request 
                                               returningResponse:&response error:nil]; 
    int statusCode =  (int)[response statusCode];
    
    NSString* strRet = nil;
    if (statusCode == 200) {
        strRet = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    } else {
        // 服务器请求失败
        strRet = @"-100";
    }
    
    // 释放对象
    //[request release];
    //return [strRet autorelease];
    return strRet;
}


-(void)test{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
    });
}

@end
